﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using Wpf2.Models;
using Wpf2.ViewModels;

namespace Wpf2.Views
{
    /// <summary>
    /// Interaction logic for formQuanlykho.xaml
    /// </summary>
    public partial class FormQuanlykho 
    {
        NotifiableCollection<Catalogs> _catalogs;
        NotifiableCollection<Unit> _units;
        NotifiableCollection<Products> _products;
        public IList<Products> ProductsShow;
        IList<Products> _productFirstLoad;
        IList<int> _hideColumns;
        public FormQuanlykho()
        {
            try
            {
                InitializeComponent();
                Inittial();
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void Inittial()
        {
            //Load Câtlog
            try
            {
                var listCa = WPFServices.Services.Catalogs.GetListCatalogs().Select(x => new Catalogs
        {
            IDCatalog = x.ID_CATALOG,
            CatalogName = x.NAME_CATALOG,
            CatalogNotes = x.NOTES_CATALOG
        });
                _catalogs = new NotifiableCollection<Catalogs>();
                foreach (Catalogs c in listCa)
                {
                    _catalogs.Add(c);
                }
                lvCatalog.DataContext = _catalogs;


                var listUN = WPFServices.Services.Units.GetListUnits().Select(x => new Unit
                {
                    UnitID = x.ID_UNIT,
                    UnitName = x.NAME_UNIT,
                    UnitNote = x.NOTES_UNIT
                });
                _units = new NotifiableCollection<Unit>();
                foreach (Unit u in listUN)
                {
                    _units.Add(u);
                }



                //load product
                _products = new NotifiableCollection<Products>();
                _productFirstLoad = new List<Products>();
                ProductsShow = new List<Products>();
                var listPro = WPFServices.Services.Products.GetListProducts().Select(p => new Products
                {
                    ProductIdAuto = p.ID_PRO_AUTO,
                    ProductId = p.ID_PRO,
                    ProductName = p.NAME_PRO,
                    ProductDescription = p.DESCRIPTIONS,
                    CostPrice = p.DETAILS_PRODUCTS.COST_PRICES ?? 0,
                    SellPrice = p.DETAILS_PRODUCTS.SELL_PRICE ?? 0,
                    InventoryCount = p.DETAILS_PRODUCTS.INVENTORY_COUNT ?? 0,
                    CatalogId = p.ID_CATALOG,
                    UnitId = p.DETAILS_PRODUCTS.ID_UNITS,
                    CatalogName = p.CATALOG.NAME_CATALOG,
                    UnitName = p.DETAILS_PRODUCTS.UNIT.NAME_UNIT

                });
                foreach (var p in listPro)
                {
                    //if (p.CatalogId == selectedCatalog.id_catalog)
                    _products.Add(p);
                    _productFirstLoad.Add(p);

                }
                dtgcmbUnit.ItemsSource = _units;
                dtgcmbCatalog.ItemsSource = _catalogs;
                dtgProduct.DataContext = _products.Where(p => p.CatalogId == _catalogs[0].IDCatalog);

                _hideColumns = new List<int>();
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void BtnSumClick(object sender, RoutedEventArgs e)
        {
            if (stpPasswordCheck.Visibility == Visibility.Visible)
                stpPasswordCheck.Visibility = Visibility.Collapsed;
            stpPassword.Visibility = Visibility.Visible;
           
        }

        private void LvCatalogSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var listView = sender as ListView;
                if (listView == null) return;
                var selectedCatalog = (listView.SelectedItem as Catalogs);
                if (selectedCatalog == null) return;
                var p = _productFirstLoad.Where(x => x.CatalogId == selectedCatalog.IDCatalog);
                dtgProduct.DataContext = p;
                ProductsShow.Clear();
                foreach (var pr in p)
                {
                    //if (p.CatalogId == selectedCatalog.id_catalog)
                    ProductsShow.Add(pr);

                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }
        private void DtgProductLoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString(CultureInfo.InvariantCulture);
        }

      
/*
        private void CheckBoxUnchecked1(object sender, RoutedEventArgs e)
        {
            stpHideShowColumn.Visibility = Visibility.Collapsed;
        }
*/
        private void CheckBoxChecked2(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[1].Visibility = Visibility.Hidden;
            if (!_hideColumns.Contains(1))
                _hideColumns.Add(1);
        }

        private void CheckBoxUnchecked2(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[1].Visibility = Visibility.Visible;
            if (_hideColumns.Contains(1))
                _hideColumns.Remove(1);
        }

        private void CheckBoxChecked3(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[2].Visibility = Visibility.Hidden;
            if (!_hideColumns.Contains(2))
                _hideColumns.Add(2);
        }

        private void CheckBoxUnchecked3(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[2].Visibility = Visibility.Visible;
            if (_hideColumns.Contains(2))
                _hideColumns.Remove(2);
        }

        private void CheckBoxChecked4(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[3].Visibility = Visibility.Hidden;
            if (!_hideColumns.Contains(3))
                _hideColumns.Add(3);
        }

        private void CheckBoxUnchecked4(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[3].Visibility = Visibility.Visible;
            if (_hideColumns.Contains(3))
                _hideColumns.Remove(3);
        }

        private void CheckBoxChecked5(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[4].Visibility = Visibility.Hidden;
            if (!_hideColumns.Contains(4))
                _hideColumns.Add(4);
        }

        private void CheckBoxUnchecked5(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[4].Visibility = Visibility.Visible;
            if (_hideColumns.Contains(4))
                _hideColumns.Remove(4);
        }

        private void CheckBoxChecked6(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[5].Visibility = Visibility.Hidden;
            if (!_hideColumns.Contains(5))
                _hideColumns.Add(5);
        }

        private void CheckBoxUnchecked6(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[5].Visibility = Visibility.Visible;
            if (_hideColumns.Contains(5))
                _hideColumns.Remove(5);
        }

        private void CheckBoxChecked7(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[6].Visibility = Visibility.Hidden;
            if (!_hideColumns.Contains(6))
                _hideColumns.Add(6);
        }

        private void CheckBoxUnchecked7(object sender, RoutedEventArgs e)
        {
             dtgProduct.Columns[6].Visibility = Visibility.Visible;
             if (_hideColumns.Contains(6))
                 _hideColumns.Remove(6);
        
        }

        private void CheckBoxChecked8(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[7].Visibility = Visibility.Hidden;
            if (!_hideColumns.Contains(7))
                _hideColumns.Add(7);
        }

        private void CheckBoxUnchecked8(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[7].Visibility = Visibility.Visible;
            if (_hideColumns.Contains(7))
                _hideColumns.Remove(7);
        }

        private void CheckBoxChecked9(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[8].Visibility = Visibility.Hidden;
            if (!_hideColumns.Contains(8))
                _hideColumns.Add(8);
        }

        private void CheckBoxUnchecked9(object sender, RoutedEventArgs e)
        {
            dtgProduct.Columns[8].Visibility = Visibility.Visible;
            if (_hideColumns.Contains(8))
                _hideColumns.Remove(8);
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var v = new ManageInventoryEditPrint(ProductsShow, _hideColumns);
                v.Show();
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void TxtPasswordPreviewKeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key != Key.Enter) return;
                var u = new WPFServices.USER
                            {
                                PASSWORDS = txtPassword.Password.Trim()
                            };

                if (WPFServices.Services.UserService.CheckPassUser(u))
                {

                    stpPassword.Visibility = Visibility.Collapsed;
                    var addpro1 = new AddProductType1();
                    addpro1.ShowDialog();
                    Inittial();
                }
                else
                {
                    MessageBox.Show("Sai mật khẩu");
                }
                txtPassword.Password = string.Empty;
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void BtnShowHideClick(object sender, RoutedEventArgs e)
        {
            if (stpPassword.Visibility == Visibility.Visible)
                stpPassword.Visibility = Visibility.Collapsed;
            if (stpHideShowColumn.Visibility == Visibility.Visible)
                stpHideShowColumn.Visibility = Visibility.Collapsed;
            stpPasswordCheck.Visibility = Visibility.Visible;
        }

        private void TxtPasswordCheckPreviewKeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    var u = new WPFServices.USER
                    {
                        PASSWORDS = txtPasswordCheck.Password.Trim()
                    };

                    if (WPFServices.Services.UserService.CheckPassUser(u))
                    {
                        stpHideShowColumn.Visibility = Visibility.Visible;
                        txtPasswordCheck.Password = string.Empty;
                        stpPasswordCheck.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        MessageBox.Show("Sai mật khẩu");
                    }
                    txtPassword.Password = string.Empty;
                }
            }
            catch (Exception ex)
            {
                
               MessageBox.Show(ex.Message); 
            }
        }

        private void TxtIDProTextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                var searchString = txtIDPro.Text;
                cmbSearchByID.Items.Clear();
                foreach (var cbmItem in from pro in _productFirstLoad where pro.ProductId.ToUpper().IndexOf(searchString.ToUpper(), StringComparison.Ordinal) >= 0 && !string.IsNullOrEmpty(searchString) select new ComboBoxItem {Content = pro.ProductId, Tag = pro.ProductIdAuto})
                {
                    cmbSearchByID.Items.Add(cbmItem);
                }
                cmbSearchByID.IsDropDownOpen = cmbSearchByID.HasItems;
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void ComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var comboBox = sender as ComboBox;
                if (comboBox != null && comboBox.SelectedItem == null) return;
                var box = sender as ComboBox;
                if (box != null)
                {
                    var name = box.SelectedItem as ComboBoxItem;
                    if (name != null) txtIDPro.Text = name.Content.ToString();
                }
                btnSearchbyid.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }
        private IList<Products> SearchPro(string value, int type)
        {
            try
            {
                if (type == 0)
                {
                    var listPro = _productFirstLoad.Where(x => x.ProductId.ToUpper().Contains(value.ToUpper().Trim()));
                    if (!string.IsNullOrEmpty(value))
                    {
                        listPro = listPro.Where(x => x.ProductId.ToUpper().Contains(value.ToUpper().Trim()));
                    }
                    return listPro.ToList();
                }
                if (type == 1)
                {
                    var listPro = _productFirstLoad.Where(x => x.ProductName.ToUpper().Contains(value.ToUpper().Trim()));
                    if (!string.IsNullOrEmpty(value))
                    {
                        listPro = listPro.Where(x => x.ProductName.ToUpper().Contains(value.ToUpper().Trim()));
                    }
                    return listPro.ToList();
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }

            return new List<Products>();
           
        }

        private void BtnSearchbyidClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var listP = SearchPro(txtIDPro.Text, 0);
                _products.Clear();
                foreach (Products p in listP)
                {
                    _products.Add(p);
                }
                cmbSearchByID.IsDropDownOpen = false;
                dtgProduct.DataContext = _products;
                ProductsShow.Clear();
                foreach (var pr in listP)
                {
                    //if (p.CatalogId == selectedCatalog.id_catalog)
                    ProductsShow.Add(pr);
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void TxtSearchbyNameTextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                var searchString = txtSearchbyName.Text;
                cmbSearchByName.Items.Clear();
                foreach (var pro in _productFirstLoad)
                {
                    if (pro.ProductName.ToUpper().IndexOf(searchString.ToUpper(), StringComparison.Ordinal) < 0 ||
                        string.IsNullOrEmpty(searchString)) continue;
                    var cbmItem = new ComboBoxItem {Content = pro.ProductName, Tag = pro.ProductIdAuto};
                    cmbSearchByName.Items.Add(cbmItem);
                }
                cmbSearchByName.IsDropDownOpen = cmbSearchByName.HasItems;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void CmbSearchByNameSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var comboBox = sender as ComboBox;
                if (comboBox != null && comboBox.SelectedItem == null) return;
                var box = sender as ComboBox;
                if (box != null)
                {
                    var name = box.SelectedItem as ComboBoxItem;
                    if (name != null) txtSearchbyName.Text = name.Content.ToString();
                }
                btnSearchbyName.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        private void BtnSearchbyNameClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var listP = SearchPro(txtSearchbyName.Text, 1);
                _products.Clear();
                foreach (var p in listP)
                {
                    _products.Add(p);
                }
                ProductsShow.Clear();
                foreach (var pr in listP)
                {
                    ProductsShow.Add(pr);

                }
                cmbSearchByName.IsDropDownOpen = false;
                dtgProduct.DataContext = _products;
            }
            catch (Exception ex)
            {
                
                 MessageBox.Show(ex.Message);
            }

        }

        private void BtnCheckPassClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var u = new WPFServices.USER
        {
            PASSWORDS = txtPassword.Password.Trim()
        };

                if (WPFServices.Services.UserService.CheckPassUser(u))
                {

                    stpPassword.Visibility = Visibility.Collapsed;
                    var addpro1 = new AddProductType1();
                    addpro1.ShowDialog();
                    Inittial();
                }
                else
                {
                    MessageBox.Show("Sai mật khẩu");
                }
                txtPassword.Password = string.Empty;
            }
            catch (Exception ex)
            {
                
                 MessageBox.Show(ex.Message);
            }
        }

        private void BtnCheckPassHideShowClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var u = new WPFServices.USER
        {
            PASSWORDS = txtPasswordCheck.Password.Trim()
        };

                if (WPFServices.Services.UserService.CheckPassUser(u))
                {
                    stpHideShowColumn.Visibility = Visibility.Visible;
                    txtPasswordCheck.Password = string.Empty;
                    stpPasswordCheck.Visibility = Visibility.Collapsed;
                }
                else
                {
                    MessageBox.Show("Sai mật khẩu");
                }
                txtPassword.Password = string.Empty;
            }
            catch (Exception ex)
            {
                 MessageBox.Show(ex.Message);
            }
        }

        private void TxtIDProPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Down)
            {
                cmbSearchByID.Focus();
            }
            
        }

        private void txtIDPro_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        private void txtSearchbyName_LostFocus(object sender, RoutedEventArgs e)
        {

        }
    }
}
